import connect from "@/utils/mongodb";
import Comment from "@/model/Comment";
import { NextResponse } from "next/server";

// 删除评论（DELETE请求）
export const DELETE = async (request, { params }) => {
    try {
        await connect();
        const { id } = await params;

        const deletedComment = await Comment.findByIdAndDelete(id);
        if (!deletedComment) {
            return NextResponse.json({
                message: "评论不存在或已删除",
                success: false
            }, { status: 404 });
        }

        return NextResponse.json({
            message: "评论删除成功",
            success: true,
            data: { id }
        });
    } catch (error) {
        console.error('删除评论失败:', error);
        return NextResponse.json({
            message: "删除评论失败",
            success: false,
            error: error.message
        }, { status: 500 });
    }
};

// 获取单条评论详情（GET请求）
export const GET = async (request, { params }) => {
    try {
        await connect();
        const { id } = await params;
        // 1. 验证 id 是否存在
        if (!id) {
            return NextResponse.json({
                message: "评论 ID 不能为空",
                success: false
            }, { status: 400 });
        }


        const comment = await Comment.findById(id);
        if (!comment) {
            return NextResponse.json({
                message: "评论不存在",
                success: false
            }, { status: 404 });
        }

        return NextResponse.json({
            message: "获取评论详情成功",
            success: true,
            data: comment
        });
    } catch (error) {
        console.error('获取评论详情失败:', error);
        return NextResponse.json({
            message: "获取评论详情失败",
            success: false,
            error: error.message
        }, { status: 500 });
    }
};